﻿using System;
using System.Data.SqlClient;
using System.Data;

namespace MvePublicService.IdeBasketOperations
{
	/*
	 * 日本SGI株式会社
	 * ヨハンソン・ヘンリク
	 * johansson@sgi.co.jp
	 *
	 * Comments:
	 *		01/07/2009	
	 *		Currently I have no Idea what an IdeaBasket is,
	 *		but since there were Ideabasket related operations in the original
	 *		service I decided to include them here as well...
	 *		
	 *		01/09/2009
	 *		There is no ideaBasket. Ideabasket keywords are added to the keyword
	 *		table with a segmentId and clipId of Null.
	 *		
	 *		01/21/2009
	 *		Ideabasket functionality is no longer implemented in the Client.
	 *		
	 * Change Log:
	 * ---------------------------------------------------------------------------
	 * 2009.01.07	Johansson Henrik		Class Created.
	 * 2009.01.09							Implemented 
	 *										AddKeywordToUserIdeaBasket,
	 *										DeleteKeywordFromUsersIdeaBasket
	 */

	/// <summary>
	/// This static class provides methods for extracting, 
	/// updating and deleting Keywords to/from IdeBaskets in the Database.
	/// Use the static methods in this class to perform
	/// </summary>
	/// <remarks>INSERT:Creds</remarks>
	internal static class IdeBasketManipulator
	{
		/// <summary>
		/// Add an IdeaBasket keyword to the DB.
		/// Original Name = AddKeywordToIdeaBasket
		/// This method adds a keyword whose segment_id is set to null.
		/// </summary>
		/// <remarks>Done</remarks>
		/// <param name="userDbId">User DB id</param>
		/// <param name="keyword">The keyword to add</param>
		/// <param name="connection">Sql Connection</param>
		/// <returns>True if the keyword was added</returns>
		internal static bool AddKeywordToUserIdeaBasket(
			int userDbId, string keyword, SqlConnection connection)
		{
			SqlCommand command = new SqlCommand();
			command.CommandTimeout = 60;

			bool result = false;
			try
			{
				command.Connection = connection;
				command.CommandType = System.Data.CommandType.StoredProcedure;
				command.CommandText = "AddIdeaBasketKeyword";

				command.Parameters.Add("userId", SqlDbType.Int).Value = userDbId;
				command.Parameters.Add("keyword", SqlDbType.VarChar, 64).Value = keyword;

				SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
				returnValue.Direction = ParameterDirection.ReturnValue;
				command.Parameters.Add(returnValue);

				command.ExecuteNonQuery();
				int resultCode = (int)returnValue.Value;

				if (resultCode == 0)
				{
					result = true;
				}
			}
			catch (InvalidOperationException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = false;
			}
			catch (InvalidCastException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = false;
			}
			finally
			{
				command.Dispose();
			}

			return result;
		}//end:AddKeywordToUserIdeaBasket

		/// <summary>
		/// Delete an IdeaBasket Keyword from the DB.
		/// Original Name = DeleteIdeaBasketKeyword
		/// </summary>
		/// <remarks>Done</remarks>
		/// <param name="userDbId">User DB id</param>
		/// <param name="keyword">The keyword to delete</param>
		/// <param name="connection">Sql Connection</param>
		/// <returns>True if the keyword was deleted</returns>
		internal static bool DeleteKeywordFromUsersIdeaBasket(
			int userDbId, string keyword, SqlConnection connection)
		{
			SqlCommand command = new SqlCommand();
			command.CommandTimeout = 60;

			bool result = false;
			try
			{
				command.Connection = connection;
				command.CommandType = System.Data.CommandType.StoredProcedure;
				command.CommandText = "DeleteIdeaBasketKeyword";

				command.Parameters.Add("userId", SqlDbType.Int).Value = userDbId;
				command.Parameters.Add("keyword", SqlDbType.Text).Value = keyword;

				SqlParameter returnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
				returnValue.Direction = ParameterDirection.ReturnValue;
				command.Parameters.Add(returnValue);

				command.ExecuteNonQuery();
				int resultCode = (int)returnValue.Value;

				if (resultCode == 0)
				{
					result = true;
				}
			}
			catch (InvalidOperationException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = false;
			}
			catch (InvalidCastException ex)
			{
				MvePublicService.ErrorHandler.LogMessage(ex.Message, false);
				result = false;
			}
			finally
			{
				command.Dispose();
			}
			return result;
		}//end:DeleteKeywordFromUsersIdeaBasket
	}
}
